using System;
using Coderoom.ScrumPlanner.Data.Repositories.Xml;
using Coderoom.ScrumPlanner.Domain;
using Coderoom.ScrumPlanner.Tests.Integration.Internal;
using Xunit;

namespace Coderoom.ScrumPlanner.Tests.Integration.XmlRepositoryTests
{
	public class XmlProjectRepositoryTests
	{
		private readonly XmlProjectRepository _repository;

		public XmlProjectRepositoryTests()
		{
			XmlRepositoryTestHelper.ResetProjectDataFile();
			_repository = new XmlProjectRepository();
		}

		[Fact]
		public void GetProjects_returns_ProjectCollection()
		{
			// act
			var projects = _repository.GetProjects();

			// assert
			Assert.NotNull(projects);
			Assert.IsType<ProjectCollection>(projects);
			Assert.Equal(3, projects.Count);

			Assert.Equal(new Guid("CF4F74C1-CE66-4B06-8480-B34445906423"), projects[0].Id);
			Assert.Equal("Project A", projects[0].Title);
			Assert.Equal("project-a", projects[0].Slug);

			Assert.Equal(new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6"), projects[1].Id);
			Assert.Equal("Project B", projects[1].Title);
			Assert.Equal("project-b", projects[1].Slug);

			Assert.Equal(new Guid("7E97578E-214E-4792-8B41-5D378E59AB2B"), projects[2].Id);
			Assert.Equal("Project X", projects[2].Title);
			Assert.Equal("project-x", projects[2].Slug);
		}

		[Fact]
		public void GetProject_by_id_returns_Project_when_project_is_found()
		{
			// arrange
			var id = _repository.GetProjects()[1].Id;

			// act
			var project = _repository.GetProject(id);

			// assert
			Assert.NotNull(project);
			Assert.IsType<Project>(project);
			Assert.Equal(id, project.Id);
			Assert.Equal("Project B", project.Title);
			Assert.Equal("project-b", project.Slug);
		}

		[Fact]
		public void GetProject_by_id_returns_null_when_project_is_not_found()
		{
			// arrange
			var id = Guid.Empty;

			// act
			var project = _repository.GetProject(id);

			// assert
			Assert.Null(project);
		}

		[Fact]
		public void GetProject_by_slug_returns_Project_when_project_is_found()
		{
			// arrange
			var slug = _repository.GetProjects()[1].Slug;

			// act
			var project = _repository.GetProject(slug);

			// assert
			Assert.NotNull(project);
			Assert.IsType<Project>(project);
			Assert.Equal(new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6"), project.Id);
			Assert.Equal("Project B", project.Title);
			Assert.Equal(slug, project.Slug);
		}

		[Fact]
		public void GetProject_by_slug_returns_null_when_project_is_not_found()
		{
			// arrange
			var slug = "";

			// act
			var project = _repository.GetProject(slug);

			// assert
			Assert.Null(project);
		}

		[Fact]
		public void CreateProject_returns_void_when_project_is_created()
		{
			// arrange
			Assert.Equal(3, _repository.GetProjects().Count);

			var newProject = new Project();
			newProject.Title = "Project C";
			newProject.Slug = "project-c";

			// act
			_repository.CreateProject(newProject);

			// assert
			var projects = _repository.GetProjects();

			Assert.Equal(4, projects.Count);

			Assert.NotEqual(Guid.Empty, projects[3].Id);
			Assert.Equal("Project C", projects[3].Title);
			Assert.Equal("project-c", projects[3].Slug);
		}

		[Fact]
		public void DeleteProject_by_id_returns_true_when_project_is_deleted()
		{
			// arrange
			Assert.Equal(3, _repository.GetProjects().Count);

			var id = _repository.GetProjects()[1].Id;

			// act
			var result = _repository.DeleteProject(id);

			// assert
			Assert.True(result);
			Assert.Equal(2, _repository.GetProjects().Count);
		}

		[Fact]
		public void DeleteProject_by_id_returns_false_when_project_is_not_deleted()
		{
			// arrange
			Assert.Equal(3, _repository.GetProjects().Count);

			var id = Guid.Empty;

			// act
			var result = _repository.DeleteProject(id);

			// assert
			Assert.False(result);
			Assert.Equal(3, _repository.GetProjects().Count);
		}
	}
}